package lol.clann.bukkit.pluginboot.newcommand;

import lol.clann.bukkit.pluginboot.model.rawmessage.RawMessage;
import lol.clann.bukkit.pluginboot.util.PageUtils;
import lol.clann.bukkit.pluginboot.util.PlayerUtils;
import lol.clann.bukkit.pluginboot.util.ServerUtils;
import lol.clann.bukkit.pluginboot.util.TextUtils;
import lol.clann.clannboot.annotation.inject.Inject;
import lol.clann.clannboot.annotation.load.PostLoad;
import lol.clann.clannboot.annotation.load.Unload;
import lol.clann.clannboot.command.CommandTree;
import lol.clann.clannboot.command.api.CommandExecutor;
import lol.clann.clannboot.command.api.ICommandSender;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.slf4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author pyz
 * @date 2018/11/27 9:42 PM
 */
public abstract class BukkitCommand extends CommandTree {

    /**
     * 是否是根指令
     */
    private final BukkitCommandAdapter adpter;
    @Inject
    private Logger $logger;
    @Inject
    private PlayerUtils $playerUtils;
    @Inject
    private TextUtils $textUtils;
    @Inject
    private PageUtils $pageUtils;
    @Inject
    private ServerUtils $serverUtils;

    // 主命令
    public BukkitCommand(String name) {
        this(name, null, new ArrayList<>());
    }

    // 主命令
    public BukkitCommand(String name, List<String> alias) {
        this(name, null, alias);
    }

    // 子命令
    public BukkitCommand(String name, Class<? extends CommandTree> parentClass) {
        this(name, parentClass, new ArrayList<>());
    }

    private BukkitCommand(String name, Class<? extends CommandTree> parentClass, List<String> alias) {
        super(name, parentClass, "", alias, "/");
        adpter = new BukkitCommandAdapter(this);
    }

    @Override
    protected void help(ICommandSender sender) {
        List<RawMessage> helps = Stream.concat(
                childMap.values().stream().map(CommandTree::getSingleCommand),
                directMap.values().stream()
        )
                .filter(sub -> hasPermission(sub, sender, false))
                .map(sub -> getHelpMessage(sub))
                .collect(Collectors.toList());
        $pageUtils.sendPage((CommandSender) sender.getSender(), helps, "§6===============§b帮助§6===============");
    }

    protected RawMessage getHelpMessage(CommandExecutor executor) {
        return RawMessage.createRawMessage(("§6/" + this.getName() + " §b" + executor.getName() + "§3 " + executor.getArgs()).trim())
                .suggestCommand("/" + this.getName() + " " + executor.getName())
                .then("    §6-§a " + executor.getDesc());
    }

    @PostLoad
    private void load$$$() {
        if (getParentClass() != null) {
            return;
        }
        $serverUtils.getCommandMap().register(getModule().getId(), adpter);

        $logger.info("注册指令:" + this.getName());
        help(new BukkitCommandSender(Bukkit.getConsoleSender()));
    }

    @Unload
    public void $unload() {
        adpter.unregister($serverUtils.getCommandMap());
    }
}
